﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 仓库堆场列表数据
    /// </summary>
    public class 仓库堆场列表数据
    {
        public static void Run(string filePath, string destFileName)
        {
            Dictionary<string, List<string>> nameDir = new Dictionary<string, List<string>>();
            Dictionary<string, List<double>> valueDir = new Dictionary<string, List<double>>();

            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder geoBuilder = new StringBuilder();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                //string nameString = Convert.ToString(dataTable.Rows[index][1]).Trim()
                //    + Convert.ToString(dataTable.Rows[index][2]).Trim();
                string companyString = Convert.ToString(dataTable.Rows[index][2]).Trim();
                string nameString = Convert.ToString(dataTable.Rows[index][3]).Trim();
                string valueString = Convert.ToString(dataTable.Rows[index][12]).Trim();
                string provinceString = Convert.ToString(dataTable.Rows[index][14]).Trim().Replace("省", string.Empty);

                if (string.IsNullOrEmpty(nameString)|| string.Equals(nameString, "无"))
                {
                    nameString = string.Format("{0}堆场", companyString);
                }

                if (string.IsNullOrEmpty(nameString)
                    || string.IsNullOrEmpty(valueString))
                    continue;

                double value = 0;
                if (!double.TryParse(valueString, out value))
                {
                    continue;
                }

                bool isInsert = false;
                if (nameDir.ContainsKey(provinceString))
                {
                    List<string> names = nameDir[provinceString];
                    List<double> values = valueDir[provinceString];
                    for (int vIndex = 0; vIndex < values.Count; vIndex++)
                    {
                        if (value > values[vIndex])
                        {
                            values.Insert(vIndex, value);
                            names.Insert(vIndex, nameString);
                            isInsert = true;
                            break;
                        }
                    }
                    if (isInsert == false)
                    {
                        nameDir[provinceString].Add(nameString);
                        valueDir[provinceString].Add(value);

                        isInsert = true;
                    }
                }
                if (isInsert == false)
                {
                    nameDir.Add(provinceString, new List<string>());
                    valueDir.Add(provinceString, new List<double>());
                    nameDir[provinceString].Add(nameString);
                    valueDir[provinceString].Add(value);
                }
            }

            StringBuilder contentBuilder = new StringBuilder();
            foreach (var key in nameDir.Keys)
            {
                StringBuilder nameBuilder = new StringBuilder();
                StringBuilder valueBuilder = new StringBuilder();
                List<string> names = nameDir[key];
                List<double> values = valueDir[key];
                for(int index = 0; index < names.Count; index++)
                {
                    if(index == 0)
                    {
                        nameBuilder.AppendFormat("\"{0}\"", names[index]);
                        valueBuilder.AppendFormat("{0}", values[index]);
                    }
                    else
                    {
                        nameBuilder.AppendFormat(",\"{0}\"", names[index]);
                        valueBuilder.AppendFormat(",{0}", values[index]);
                    }
                }
                if(contentBuilder.Length ==0)
                {
                    contentBuilder.AppendFormat("\"{0}\":[{1}]", key, nameBuilder);
                    contentBuilder.AppendFormat(",\"{0}V\":[{1}]", key, valueBuilder);
                }
                else
                {
                    contentBuilder.AppendFormat(",\"{0}\":[{1}]", key, nameBuilder);
                    contentBuilder.AppendFormat(",\"{0}V\":[{1}]", key, valueBuilder);
                }
            }

            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("{{{0}}}", contentBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}